package com.cskaoyan.mall.controller.admin;

import com.alibaba.druid.util.StringUtils;
import com.cskaoyan.mall.model.goods.*;
import com.cskaoyan.mall.model.message.Reply;
import com.cskaoyan.mall.model.message.ReplyMsgVO;
import com.cskaoyan.mall.service.GoodsService;
import com.cskaoyan.mall.service.GoodsServiceImpl;
import com.cskaoyan.mall.util.FileUploadUtils;
import com.cskaoyan.mall.util.RequestBodyUtils;
import com.cskaoyan.mall.util.Result;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @author: jyf2000jyf
 */
@WebServlet("/api/admin/goods/*")
public class GoodsServlet extends HttpServlet {
    private GoodsService goodsService = new GoodsServiceImpl();
    private Gson gson = new Gson();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String requestURI = request.getRequestURI();
        String op = requestURI.replace(request.getContextPath() + "/api/admin/goods/", "");
        if ("imgUpload".equals(op)) {
            imgUpload(request, response);
        } else if ("addGoods".equals(op)) {
            addGoods(request, response);
        } else if ("addType".equals(op)) {
            addType(request, response);
        } else if ("updateGoods".equals(op)) {
            updateGoods(request, response);
        } else if ("addSpec".equals(op)) {
            addSpec(request, response);
        } else if ("deleteSpec".equals(op)) {
            deleteSpec(request, response);
        } else if ("reply".equals(op)) {
            reply(request, response);
        }
    }

    private void imgUpload(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Map<String, String> Map = FileUploadUtils.parseRequest(request);
        String file = Map.get("file");
        Result result = new Result(0, null, file);
        response.getWriter().println(gson.toJson(result));
    }

    private void addGoods(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody = RequestBodyUtils.getRequestBody(request);
        AddGoodsBO addGoodsBO = null;
        Result result;
        try {
            addGoodsBO = gson.fromJson(requestBody, AddGoodsBO.class);
        } catch (Exception e) {
            result = new Result(10000, "参数需要为数字", null);
            response.getWriter().println(gson.toJson(result));
            return;
        }
        boolean status = goodsService.addGoods(addGoodsBO);
        if (status) {
            result = new Result(0, "插入成功", null);
        } else {
            result = new Result(10000, "插入失败", null);
        }
        response.getWriter().println(gson.toJson(result));
    }

    private void addType(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody = RequestBodyUtils.getRequestBody(request);
        Type type = gson.fromJson(requestBody, Type.class);
        boolean status = goodsService.addType(type);
        Result result;
        if (status) {
            result = new Result(0, "插入成功", null);
        } else {
            result = new Result(10000, "插入失败，已有重复数据", null);
        }
        response.getWriter().println(gson.toJson(result));
    }

    private void updateGoods(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody = RequestBodyUtils.getRequestBody(request);
        UpdateGoodsBO updateGoodsBO = gson.fromJson(requestBody, UpdateGoodsBO.class);
        boolean status = goodsService.updateGoods(updateGoodsBO);
        Result result;
        if (status) {
            result = new Result(0, "修改成功", null);
        } else {
            result = new Result(10000, "修改失败", null);
        }
        response.getWriter().println(gson.toJson(result));
    }

    private void addSpec(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody = RequestBodyUtils.getRequestBody(request);
        Spec spec = gson.fromJson(requestBody, Spec.class);
        boolean status = goodsService.addSpec(spec);
        Result result;
        if (status) {
            result = new Result(0, "插入成功", spec);
        } else {
            result = new Result(10000, "插入失败,分类名重复", null);
        }
        response.getWriter().println(gson.toJson(result));
    }

    private void deleteSpec(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody = RequestBodyUtils.getRequestBody(request);
        Spec spec = gson.fromJson(requestBody, Spec.class);
        boolean status = goodsService.deleteSpec(spec);
        Result result;
        if (status) {
            result = new Result(0, "删除成功", null);
        } else {
            result = new Result(10000, "删除失败", null);
        }
        response.getWriter().println(gson.toJson(result));
    }

    private void reply(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody = RequestBodyUtils.getRequestBody(request);
        Reply replyContent = gson.fromJson(requestBody, Reply.class);
        boolean status = goodsService.reply(replyContent);
        Result result;
        if (status) {
            result = new Result(0, "修改成功", null);
        } else {
            result = new Result(10000, "修改失败", null);
        }
        response.getWriter().println(gson.toJson(result));
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String requestURI = request.getRequestURI();
        String op = requestURI.replace(request.getContextPath() + "/api/admin/goods/", "");
        if ("getType".equals(op)) {
            getType(request, response);
        } else if ("getGoodsByType".equals(op)) {
            getGoodsByType(request, response);
        } else if ("deleteGoods".equals(op)) {
            deleteGoods(request, response);
        } else if ("deleteType".equals(op)) {
            deleteType(request, response);
        } else if ("getGoodsInfo".equals(op)) {
            getGoodsInfo(request, response);
        } else if ("noReplyMsg".equals(op)) {
            getNoReplyMsg(request, response);
        } else if ("repliedMsg".equals(op)) {
            getRepliedMsg(request, response);
        }
    }

    private void getType(HttpServletRequest request, HttpServletResponse response) throws IOException {
        List<Type> typeList = goodsService.getType();
        Result result = new Result(0, null, typeList);
        response.getWriter().println(gson.toJson(result));
    }

    private void getGoodsByType(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String typeId = request.getParameter("typeId");
        Result result;
        if (StringUtils.isEmpty(typeId)) {
            result = new Result(10000, "不能为空", null);
            response.getWriter().println(gson.toJson(result));
            return;
        }
        List<GoodsByTypeVO> goodsByTypeVOList = goodsService.getGoodsByType(Integer.parseInt(typeId));
        result = new Result(0, null, goodsByTypeVOList);
        response.getWriter().println(gson.toJson(result));
    }


    private void deleteGoods(HttpServletRequest request, HttpServletResponse response) throws IOException {
        int id = Integer.parseInt(request.getParameter("id"));
        boolean status = goodsService.deleteGoods(id);
        Result result;
        if (status) {
            result = new Result(0, "删除成功", null);
        } else {
            result = new Result(10000, "删除失败", null);
        }
        response.getWriter().println(gson.toJson(result));
    }

    private void deleteType(HttpServletRequest request, HttpServletResponse response) throws IOException {
        int typeId = Integer.parseInt(request.getParameter("typeId"));
        boolean status = goodsService.deleteType(typeId);
        Result result;
        if (status) {
            result = new Result(0, "删除成功", null);
        } else {
            result = new Result(10000, "删除失败", null);
        }
        response.getWriter().println(gson.toJson(result));
    }

    private void getGoodsInfo(HttpServletRequest request, HttpServletResponse response) throws IOException {
        // id指goodsId
        int id = Integer.parseInt(request.getParameter("id"));
        GoodsInfoVO goodsInfoVO = goodsService.getGoodsInfo(id);
        Result result = new Result(0, null, goodsInfoVO);
        response.getWriter().println(gson.toJson(result));
    }

    private void getNoReplyMsg(HttpServletRequest request, HttpServletResponse response) throws IOException {
        List<ReplyMsgVO> replyMsgVOList = goodsService.getNoReplyMsg();
        Result result = new Result(0, null, replyMsgVOList);
        response.getWriter().println(gson.toJson(result));
    }

    private void getRepliedMsg(HttpServletRequest request, HttpServletResponse response) throws IOException {
        List<ReplyMsgVO> replyMsgVOList = goodsService.getRepliedMsg();
        Result result = new Result(0, null, replyMsgVOList);
        response.getWriter().println(gson.toJson(result));
    }
}
